package uri.model.collection;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import uri.model.BoardVO;
import uri.model.FileVO;

@Service
public class CollectionServiceImpl implements CollectionService {
	@Resource(name="collectionDAOImpl")
	private CollectionDAO collectionDAO;

	@Override
	public void writeCollection(BoardVO bvo) {
		collectionDAO.writeCollection(bvo);
	}

	@Override
	public void fileUpload(FileVO fvo) {
		collectionDAO.fileUpload(fvo	);
	}

	@Override
	public BoardVO detailCollectionNoHit(String no) {
		return collectionDAO.detailCollectionNoHit(no);
	}

	@Override
	public List<FileVO> fileInfo(String no) {
		return collectionDAO.fileInfo(no);
	}

	@Override
	public CollectionListVO contentList(String nowPage) {
		int nowPage1=1;
		if(nowPage!=null){
			nowPage1=Integer.parseInt(nowPage);
		}
		System.out.println("서비스 nowPage 값: "+nowPage1);
		List<BoardVO> listVO=collectionDAO.contentList(nowPage1);
		System.out.println("listVO : "+listVO);
		int total=collectionDAO.getAllContentCount();
		System.out.println("전체 게시물 수 : "+total);
		PagingBean pagingBean=new PagingBean(total, nowPage1);
		return new CollectionListVO(listVO,pagingBean);
	}

	@Override
	public BoardVO detailCollectionHitPlus(String no) {
		BoardVO bvo=detailCollectionNoHit(no);
		hitPlus(no);
		System.out.println("서비스계층 , 조회수 증가 완료 ");
		return bvo;
	}

	@Override
	public void hitPlus(String no) {
		collectionDAO.hitPlus(no);
	}

	@Override
	public void deleteCollection(String no) {
		collectionDAO.deleteCollection(no);
	}

	@Override
	public void deleteFiles(String no, String path) {
		List<String> list = collectionDAO.findFileNameByNo(no);
		for(int i=0;i<list.size();i++){
			File mFile = new File(path+list.get(i)); //파일 객체를 만들고
			boolean flag =  mFile.delete(); //실제 파일을 삭제 
			if(flag)
			System.out.println(mFile.getName()+" "+i+" 번째파일 삭제완료");
		}
		collectionDAO.deleteFiles(no);//DB의 파일 삭제 
	}
	
	@Override
	public void deleteFile(String no, String path) {
		String newFileName=collectionDAO.getFileNameByNo(no);
		File file=new File(path+newFileName);
		boolean flag=file.delete();
		if(flag){
			System.out.println("파일 한개 삭제 완료");
		}
		collectionDAO.deleteFile(no);
	}

	@Override
	public List<CommentVO> getAllComment(String no) {
		return collectionDAO.getAllComment(no);
	}

	@Override
	public int writeComment(CommentVO cvo) {
		return collectionDAO.writeComment(cvo);
	}

	@Override
	public int deleteComment(String no) {
		return collectionDAO.deleteComment(no);
	}

	@Override
	public int updateComment(CommentVO cvo) {
		return collectionDAO.updateComment(cvo);
	}

	@Override
	public void updateCollection(BoardVO bvo) {
		collectionDAO.updateCollection(bvo);
	}

	@Override
	public void updateFileList(FileVO fvo) {
		collectionDAO.updateFileList(fvo);
	}

	@Override
	public void updateFile(FileVO updateFileInfo) {
		collectionDAO.updateFile(updateFileInfo);
	}

	@Override
	public void deleteFileByno(String no) {
		collectionDAO.deleteFileByno(no);
	}

	@Override
	public void replyWriteGo(BoardVO bvo) {
		collectionDAO.updateRestep(bvo);
		bvo.setRelevel(bvo.getRelevel()+1);
		bvo.setRestep(bvo.getRestep()+1);
		bvo.setTitle("Re: "+bvo.getTitle());
		//bvo.setRef(Integer.parseInt(bvo.getNo()));
		collectionDAO.replyWriteGo(bvo);
	}


	@Override
	public void updateRef(BoardVO bvo) {
		collectionDAO.updateRef(bvo);
		
	}

	@Override
	public CollectionListVO findCollectionByTitle(String word,String nowPage) {
		int nowpage=1;
		if(nowPage!=null){
			nowpage=Integer.parseInt(nowPage);
		}
		HashMap map=new HashMap();
		map.put("nowpage", nowpage);
		map.put("word", word);
		List<BoardVO> listVO=collectionDAO.findCollectionByTitle(map);
		System.out.println("listVO : "+listVO);
		int total=listVO.size();
		System.out.println("검색된 게시물 수 : "+total);
		PagingBean pagingBean=new PagingBean(total, nowpage);
		CollectionListVO collectionListVO=new CollectionListVO(listVO,pagingBean);
		return collectionListVO;
		
	}

	@Override
	public CollectionListVO findCollectionByContent(String word, String nowPage) {
		int nowpage=1;
		if(nowPage!=null){
			nowpage=Integer.parseInt(nowPage);
		}
		HashMap map=new HashMap();
		map.put("nowpage", nowpage);
		map.put("word", word);
		List<BoardVO> listVO=collectionDAO.findCollectionByContent(map);
		System.out.println("listVO : "+listVO);
		int total=listVO.size();
		System.out.println("검색된 게시물 수 : "+total);
		PagingBean pagingBean=new PagingBean(total, nowpage);
		CollectionListVO collectionListVO=new CollectionListVO(listVO,pagingBean);
		return collectionListVO;
	}

	@Override
	public CollectionListVO findCollectionByTitleAndContent(String word,
			String nowPage) {
		int nowpage=1;
		if(nowPage!=null){
			nowpage=Integer.parseInt(nowPage);
		}
		HashMap map=new HashMap();
		map.put("nowpage", nowpage);
		map.put("word", word);
		List<BoardVO> listVO=collectionDAO.findCollectionByTitleAndContent(map);
		System.out.println("listVO : "+listVO);
		int total=listVO.size();
		System.out.println("검색된 게시물 수 : "+total);
		PagingBean pagingBean=new PagingBean(total, nowpage);
		CollectionListVO collectionListVO=new CollectionListVO(listVO,pagingBean);
		return collectionListVO;
	}

	@Override
	public void updateLike(String no) {
		collectionDAO.updateLike(no);
	}

	
	
	

	
	
}
